@extends('layouts._app')
@section('content')
    <body>
    <div class="wrap-container">
        <div class="column-content-detail">
            {!! Form::open(['route' => 'system.user.index','class'=>'layui-form','method'=>'get']) !!}
            <div class="layui-form-item">
                <div class="layui-inline tool-btn">
                    <a class="layui-btn layui-btn-small layui-btn-primary" id='email-refresh-btn'
                       href="{{route('system.user.index')}}" data-title='刷新'>
                        <i class="layui-icon">&#x1002;</i>
                    </a>
                    <button class="layui-btn layui-btn-small layui-btn-normal addBtn"
                            data-title="用户添加" data-url="{{route('system.user.create')}}">
                        <i class="layui-icon">&#xe654;</i>
                    </button>
                    <button class="layui-btn layui-btn-small layui-btn-danger delBtn hidden-xs">
                        <i class="layui-icon">&#xe640;</i>
                    </button>
                </div>
                <div class="layui-inline">
                    {!! Form::search('name',"{$request['name']}",['class'=>'layui-input','placeholder'=>'请输入名称']) !!}
                </div>
                <div class="layui-inline">
                    <select name="static" lay-filter="status">
                        <option value="-1">请选择</option>
                        <option value="1" @if(1 == Request::input('static'))
                        selected @endif>显示
                        </option>
                        <option value="2" @if(2 == Request::input('static')) selected @endif>隐藏</option>
                    </select>
                </div>
                {!! Form::submit('搜索',['class'=>'layui-btn layui-btn-normal','lay-submit'=>'search']) !!}
                <div class="layui-inline" style="float: right;">
                    <select id="change_category" lay-filter="category" lay-search data-id="users">
                        <option selected>下载</option>
                        <option>.txt</option>
                        <option>.xlsx</option>
                    </select>
                </div>
            </div>
            {!! Form::close() !!}
            @include('layouts._flash')
            <div class="layui-form" id="table-list">
                <table class="layui-table">
                    <thead>
                    <tr>
                        <th><input type="checkbox" name="" lay-skin="primary" lay-filter="allChoose"></th>
                        <th class="hidden-xs">ID</th>
                        <th>邮箱</th>
                        <th class="hidden-xs">名称</th>
                        <th class="hidden-xs">手机号</th>
                        <th class="hidden-xs">用户组</th>
                        <th class="hidden-xs">真实姓名</th>
                        <th class="hidden-xs">备注</th>
                        <th class="hidden-xs">头像</th>
                        <th class="hidden-xs">创建时间</th>
                        <th class="hidden-xs">状态</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    @foreach($user as $value)
                        <tr>
                            <td><input type="checkbox" name="del" lay-skin="primary" data-id="{{$value->id}}"></td>
                            <td class="hidden-xs" title="">{{$value->id}}</td>
                            <td title="{{$value->email}}">{{sub($value->email)}}</td>
                            <td class="hidden-xs" title="{{$value->name}}">{{sub($value->name)}}</td>
                            <td class="hidden-xs" title="{{$value->phone}}">{{sub($value->phone)}}</td>
                            <td class="hidden-xs"
                                title="{{$value->roles->implode('name','， ')}}">{{sub($value->roles->implode('name','， '))}}</td>
                            <td class="hidden-xs" title="{{$value->info->rename}}">{{$value->info->rename}}</td>
                            <td class="hidden-xs" title="{{$value->info->content}}">{{sub($value->info->content)}}</td>
                            <td class="hidden-xs" align="center"
                                title="{{$value->name}}">@if(isset($value->info->file->url))<img
                                    style="margin:-10px;width: 60px;height: 60px;border-radius: 50%;"
                                    src="{{ assetImage($value->info->file->url)}}" alt="{{$value->name}}">@endif</td>
                            <td title="{{$value->created_at}}">{{sub($value->created_at)}}</td>
                            <td class="hidden-xs">
                                <button onclick="onstatus('{{$value->id}}',{{$value->static}})"
                                        title="{{$value->static==1?'正常':'禁用'}}"
                                        class="layui-btn layui-btn-mini @if($value->static == 1)layui-btn-normal @else layui-btn-warm @endif">
                                    {{$value->static==1 ? '正常':'禁用'}}</button>
                            </td>
                            <td>
                                <div class="layui-inline">
                                    <button class="layui-btn layui-btn-small layui-btn-normal  edit-btn"
                                            data-id="{{$value->id}}" data-title="编辑" title="编辑"
                                            data-url="{{route('system.user.edit',$value->id)}}"><i
                                            class="layui-icon">&#xe642;</i></button>
                                    <a class="layui-btn layui-btn-small layui-btn-danger"
                                       href="{{route('system.user.destroy',$value->id)}}"
                                       data-method="delete" title="删除"
                                       data-token="{{csrf_token()}}" data-confirm="确认删除吗?">
                                        <i class="layui-icon">&#xe640;</i></a>
                                </div>
                            </td>
                        </tr>
                    @endforeach
                    </tbody>
                </table>
                <!--tp分页-->
                <div class="page-wrap">
                    <p style="float: left;">注 : 共 {{$user->total()}} 条记录</p>
                    {{ $user->links() }}
                </div>
            </div>
        </div>
    </div>
    <script src="{{asset('static/admin/layui/layui.js')}}" type="text/javascript" charset="utf-8"></script>
    <script src="{{asset('static/admin/js/common.js')}}" type="text/javascript" charset="utf-8"></script>
    <script src="{{asset('js/jquery.min.js')}}" type="text/javascript" charset="utf-8"></script>
    <script src="{{asset('js/destroy.js')}}" type="text/javascript" charset="utf-8"></script>
    <script src="{{asset('js/common.js')}}" type="text/javascript" charset="utf-8"></script>
    <script>
        function onstatus(id, status) {
            $.ajax({
                type: 'POST',
                url: '{{route('system.user.status')}}',
                data: {id: id, static: status},
                dataType: "json",
                beforeSend: function () {
                    layer.load(1, {
                        shade: [0.1, '#333333'] //0.1透明度的白色背景
                    });
                },
                complete: function (XHR) {
                    XHR = null;
                },
                error: function (xhr) {//获取ajax的错误信息
                    layer.alert(xhr.responseText, "Failed", function () {
                        layer.closeAll();
                    });
                },
                success: function (res) {
                    layer.closeAll();
                    if (res.code == 403) {
                        layer.msg(res.msg, {icon: 4, time: 3000})
                        return false;
                    }
                    if (res.code != 200) {
                        layer.msg(res.msg, {icon: 5, time: 3000})
                        return false;
                    }
                    layer.msg(res.msg, {icon: 1, time: 3000}, function () {
                        window.location.reload();
                    });
                }
            })
        }

        layui.use(['jquery', 'dialog', 'form'], function () {
            var $ = layui.jquery;
            var dialog = layui.dialog;
            //顶部批量删除
            $('.delBtn').click(function () {
                var ids = [];
                $("input[name='del']:checked").each(function (i) {
                    ids[i] = $(this).data('id');
                });
                if (ids.length == 0) {
                    layer.msg('请选择要删除的', {icon: 5, time: 3000});
                    return false;
                }
                dialog.confirm({
                    message: '您确定要删除选中项',
                    success: function () {
                        $.ajax({
                            type: 'POST',
                            url: '{{route('system.user.delete')}}',
                            data: {id: ids},
                            dataType: "json",
                            beforeSend: function () {
                                layer.load(1, {
                                    shade: [0.1, '#333333'] //0.1透明度的白色背景
                                });
                            },
                            complete: function (XHR) {
                                XHR = null;
                            },
                            error: function (xhr) {//获取ajax的错误信息
                                layer.alert(xhr.responseText, "Failed", function () {
                                    layer.closeAll();
                                });
                            },
                            success: function (res) {
                                layer.closeAll();
                                if (res.code == 403) {
                                    layer.msg(res.msg, {icon: 4, time: 3000})
                                    return false;
                                }
                                if (res.code != 200) {
                                    layer.msg(res.msg, {icon: 5, time: 3000})
                                    return false;
                                }
                                layer.msg(res.msg, {icon: 1, time: 3000}, function () {
                                    window.location.reload();
                                });
                            }
                        })
                    },
                    cancel: function () {
                        return false;
                    }
                })
                return false;

            }).mouseenter(function () {

                dialog.tips('批量删除', '.delBtn');

            })
            var form = layui.form();

            form.on('select(category)', function (e) {
                if (e.value == '下载') {
                    return false;
                }
                /**
                 * 写入文件下载
                 * @param id
                 */
                var table = $("#change_category").data('id');
                var ext = e.value;
                dialog.confirm({
                    message: '这可能会耗费很长时间！是否继续',
                    success: function () {
                        window.open('{{route('TextFile')}}' + '?table=' + table + '&is_show=1&ext=' + ext, '_parent');
                        window.location.reload();
                    },
                });
            });
            form.render();
        })
    </script>
    </body>
@endsection
